﻿Imports DTO

Public Class LoaiAcquyDAO
    Private ketnoi As ConnectDB

    Public Sub New()
        ketnoi = New ConnectDB()
    End Sub

    Public Function InsertUpdateLoaiAcQuy(ByVal laq As LoaiAcquyDTO, ByVal flagIsUp As Boolean)
        Dim MLoai As Integer = laq.MaLoai
        Dim TLoai As String = laq.TenLoai
        Dim sqlString As String
        'Kiem tra Flag . Neu True - Insert con Flase - Update'
        If (flagIsUp = True) Then
            sqlString = "INSERT LOAIACQUY(TenLoai) VALUES(N'" & TLoai & "')"
        Else
            sqlString = "UPDATE LOAIACQUY SET TenLoai = N'" & TLoai & "' WHERE MaLoai = " & MLoai & ""
        End If
        Return ketnoi.Execute(sqlString)
    End Function

    Public Function DeleteLoaiAcQuy(ByVal laq As LoaiAcquyDTO)
        Dim MLoai As Integer = laq.MaLoai
        Dim sqlString As String
        sqlString = "DELETE LOAIACQUY WHERE MaLoai = " & MLoai & ""
        Return ketnoi.Execute(sqlString)
    End Function

    Public Function GetData()
        Dim MLoai As Integer
        Dim TLoai As String
        Dim laq As LoaiAcquyDTO
        Dim laqList As New List(Of LoaiAcquyDTO)
        Dim dt As DataTable
        dt = ketnoi.GetData("Select * From LOAIACQUY")
        Dim i As Integer
        For i = 0 To dt.Rows.Count - 1
            MLoai = dt.Rows(i)("MaLoai").ToString
            TLoai = dt.Rows(i)("TenLoai").ToString
            laq = New LoaiAcquyDTO(MLoai, TLoai)
            laqList.Add(laq)
        Next
        Return laqList
    End Function

    Public Function TestDB(ByVal sql As String) As Boolean
        Dim dt As DataTable
        dt = ketnoi.GetData(sql)
        If (dt.Rows.Count() > 0) Then
            Return True
        Else
            Return False
        End If
    End Function
End Class
